import * as reg from '../../constants/reg';
import * as api from '../../api/user';
import { withMsg } from '../../utils/request';
import { mapState, mapActions } from 'vuex';

export default {
    data () {
        return {
            tel: '',
            authCode: '',
            sending: false,
            sendText: '发送验证码',
            delay: 60,
        };
    },
    methods: {
        ...mapActions('mine', [
            'updateUserProfile',
            'updateUserInfo',
        ]),
        goBack () {
            // 跳转到之前的页面
        },
        async onLogin (params) {
            // api.login(params)
            //     .then((res) => {
            //         Notify({ type: 'success', message: res.data.message });
            //         localStorage.setItem('TOKEN', `Bearer ${ res.data.data.token }`);
            //     })
            //     .catch((err) => {
            //         Notify({ type: 'danger', message: err.response.data.message });
            //     });

            // withMsg1( api.login(params), (res) => {
            //     localStorage.setItem('TOKEN', `Bearer ${ res.data.data.token }`);
            // } );
            const res = await withMsg( api.login(params) );
            localStorage.setItem('TOKEN', `Bearer ${ res.data.data.token }`);
            this.$router.push('/');
            this.updateUserProfile();
            this.updateUserInfo();
        },
        onSendAuthCode () {
            this.sending = true;
            const countDown = () => {
                this.delay--;
                this.sendText = this.delay;
            };
            countDown();
            const timer = setInterval(countDown, 1000);
            setTimeout(() => {
                this.sending = false;
                this.sendText = '发送验证码';
                clearTimeout(timer);
            }, 60000);
            // api.sendAuthCode(this.tel)
            //     .then((res) => {
            //         Notify({ type: 'success', message: res.data.message });
            //     })
            //     .catch((err) => {
            //         // console.dir(err);
            //         Notify({ type: 'danger', message: err.response.data.message });
            //     });
            // withMsg1( api.sendAuthCode(this.tel) );
            withMsg( api.sendAuthCode(this.tel) );
        },
        // 校验手机号的规则
        isTel: (val) => reg.TEL.test(val),
        isAuthCode: (val) => reg.AUTH_CODE.test(val),
    },
}